aboutsummaryrefslogtreecommitdiffstats
path: root/octopart.go
blob: 9c7db8975319dcfce172bb9d0bf5b7b81708f06c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main

import (
	"bytes"
	"encoding/json"
	"log"
	"net/http"
	"net/url"
    "strconv"
	//"io/ioutil"
)

/*
Routines to make (cached) API calls to Octopart and merge results into BOM
LineItems.
*/

var pricingSource *OctopartClient

type OctopartClient struct {
	ApiKey     string
	RemoteHost string
	client     *http.Client
	infoCache  map[string]interface{}
}

func NewOctopartClient(apikey string) *OctopartClient {
	oc := &OctopartClient{ApiKey: apikey,
		RemoteHost: "https://octopart.com"}
	oc.client = &http.Client{}
	oc.infoCache = make(map[string]interface{})
	return oc
}

func (oc *OctopartClient) apiCall(method string, params map[string]string) (map[string]interface{}, error) {
	paramString := "?apikey=" + oc.ApiKey
	// TODO: assert clean-ness of params
	// TODO: use url.Values instead...
	for key := range params {
		paramString += "&" + url.QueryEscape(key) + "=" + url.QueryEscape(params[key])
	}
	paramStringUnescaped, _ := url.QueryUnescape(paramString) // TODO: err
	log.Println("Fetching: " + oc.RemoteHost + "/api/v2/" + method + paramStringUnescaped)
	resp, err := oc.client.Get(oc.RemoteHost + "/api/v2/" + method + paramString)
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != 200 {
		return nil, Error("Octopart API call error: " + resp.Status)
	}
	result := make(map[string]interface{})
	defer resp.Body.Close()
	//body, err := ioutil.ReadAll(resp.Body)
	//if err != nil {
	//    return nil, err
	//}
	//body = append(body, '\n')
	//dec := json.NewDecoder(bytes.NewReader(body))
	dec := json.NewDecoder(resp.Body)
	if err = dec.Decode(&result); err != nil {
		return nil, err
	}
	return result, nil
}

// this method doesn't check internal cache, but it does append to it
func (oc *OctopartClient) bomApiCall(manufacturers, mpns []string) ([]map[string]interface{}, error) {
	// TODO: check len(mpns) == len(manufacturers) 
	queryList := make([]map[string]string, len(mpns))
	listItem := make(map[string]string)
	for i, _ := range mpns {
		listItem = make(map[string]string)
		listItem["mpn_or_sku"] = mpns[i]
		listItem["manufacturer"] = manufacturers[i]
		//listItem["q"] = mpns[i]
		listItem["limit"] = "1"
		listItem["reference"] = manufacturers[i] + "|" + mpns[i]
		queryList[i] = listItem
	}

	linesBuffer := new(bytes.Buffer)
	enc := json.NewEncoder(linesBuffer)
	if err := enc.Encode(queryList); err != nil {
		return nil, err
	}

	response, err := oc.apiCall("bom/match", map[string]string{"lines": linesBuffer.String()})
	if err != nil {
		return nil, err
	}
	// TODO: just grabbing first result for now; user can make better specification later
	ret := make([]map[string]interface{}, len(mpns))
	for i, rawresult := range response["results"].([]interface{}) {
		result := rawresult.(map[string]interface{})
        hits := int(0)
        if result["hits"] != nil {
            hits = int(result["hits"].(float64))
        }
		reference := result["reference"].(string)
		if hits == 0 {
			ret[i] = nil
			oc.infoCache[reference] = nil
		} else {
			ret[i] = result["items"].([]interface{})[0].(map[string]interface{})
			oc.infoCache[reference] = ret[i]
		}
	}
	return ret, nil
}

// this method checks the API query cache
func (oc *OctopartClient) GetMarketInfoList(manufacturers, mpns []string) ([]map[string]interface{}, error) {
	if len(mpns) < 1 {
		return nil, Error("no mpns strings passed in")
	}
	if len(mpns) != len(manufacturers) {
		return nil, Error("number of mpns doesn't match number of manufacturers")
	}
	if len(mpns) > 100 {
		return nil, Error("can't handle more than 100 queries at a time (yet)")
	}
	mpnToQuery := make([]string, 0)
	manufacturersToQuery := make([]string, 0)
	queryHash := ""
	// check for queryHashes in internal cache
	for i, _ := range mpns {
		queryHash = manufacturers[i] + "|" + mpns[i]
		if _, hasKey := oc.infoCache[queryHash]; hasKey != true {
			manufacturersToQuery = append(manufacturersToQuery, manufacturers[i])
			mpnToQuery = append(mpnToQuery, mpns[i])
		}
	}
	// if necessary, fetch missing queryHashes remotely
	for len(mpnToQuery) > 0 {
        high := len(mpnToQuery)
        if high >= 20 {
            high = 20
        }
		if _, err := oc.bomApiCall(manufacturersToQuery[0:high], mpnToQuery[0:high]); err != nil {
			return nil, err
		}
        mpnToQuery = mpnToQuery[high:len(mpnToQuery)]
        manufacturersToQuery = manufacturersToQuery[high:len(manufacturersToQuery)]
	}
	// construct list of return info
	result := make([]map[string]interface{}, len(mpns))
	for i, _ := range mpns {
		queryHash = manufacturers[i] + "|" + mpns[i]
		value, hasKey := oc.infoCache[queryHash]
		if hasKey != true {
			return nil, Error("key should be in cache, but isn't: " + queryHash)
		}
        if value == nil || mpns[i] == "" || manufacturers[i] == "" {
            result[i] = nil
        } else {
		    result[i] = value.(map[string]interface{})
        }
	}
	return result, nil
}

func (oc *OctopartClient) GetMarketInfo(manufacturer, mpn string) (map[string]interface{}, error) {
    info, err := oc.GetMarketInfoList([]string{manufacturer}, []string{mpn})
    if err != nil {
        return nil, err
    }
    return info[0], nil
}

func (oc *OctopartClient) GetExtraInfo(manufacturer, mpn string) (map[string]string, error) {
	marketInfo, err := oc.GetMarketInfo(manufacturer, mpn)
	if err != nil {
		return nil, err
	}
    // extract market price, total avail, and "availability factor" from
    // market info
    ret := make(map[string]string)
    if marketInfo != nil {
        if marketInfo["avg_price"].([]interface{})[0] != nil {
            ret["MarketPrice"] = "$" + strconv.FormatFloat(marketInfo["avg_price"].([]interface{})[0].(float64), 'f', 2, 64)
        } else {
            ret["MarketPrice"] = ""
        }
        ret["MarketFactor"] = marketInfo["market_status"].(string)
        ret["OctopartUrl"] = marketInfo["detail_url"].(string)
    } 
	return ret, nil
}

func (oc *OctopartClient) AttachMarketInfo(li *LineItem) error {
	if li.AggregateInfo == nil {
		li.AggregateInfo = make(map[string]string)
	}
	extraInfo, err := oc.GetExtraInfo(li.Manufacturer, li.Mpn)
	if err != nil {
		log.Println(err.Error())
		return err
	}
	for key := range extraInfo {
		li.AggregateInfo[key] = string(extraInfo[key])
	}
	return nil
}

func (oc *OctopartClient) AttachMarketInfoBom(b *Bom) error {
    // first ensure the cache is primed
    manufacturers := make([]string, len(b.LineItems))
    mpns := make([]string, len(b.LineItems))
    for i, li := range b.LineItems {
        manufacturers[i] = li.Manufacturer
        mpns[i] = li.Mpn
    }
    _, err := oc.GetMarketInfoList(manufacturers, mpns)
    if err != nil {
        log.Println(err.Error())
        return err
    }

    for i := range b.LineItems {
        oc.AttachMarketInfo(&(b.LineItems[i]))
    }
	return nil
}